﻿using System;
using System.Collections.Generic;
using NUnit.Framework;
using Xenta.Enums;
using Xenta.Operations;
using Xenta.Utils;

namespace Xenta.Tests.Operations
{
    [TestFixture]
    public class SalesOptionTests : OperationTest
    {
        protected override IEnumerable<Type> ModelMappings
        {
            get
            {
                return NHMappings.ALL;
            }
        }

        [TestCase("name", "value", 100)]
        public void Can_CRUD(string codeName, string codeValue, decimal price)
        {
            var option = Processor.Execute(new CreateSalesOption
            {
                CodeName = codeName,
                CodeValue = codeValue,
                Price = price,
                Flags = SalesOptionFlags.None
            }).Result;

            Assert.NotNull(option);
            option.CodeName.AreEqualIgnoringCase(codeName);
            option.CodeValue.AreEqualIgnoringCase(codeValue);
            option.Price.AreEqual(price);
            option.AreEqual(Processor.Execute(new GetSalesOption
            {
                EntityID = option.EntityID
            }).Result);

            codeName = codeName.Reverse();
            codeValue = codeValue.Reverse();
            price = price * 2;
            var upd_option = Processor.Execute(new UpdateSalesOption
            {
                EntityID = option.EntityID,
                CodeName = codeName,
                CodeValue = codeValue,
                Price = price,
                Flags = SalesOptionFlags.Item
            }).Result;

            Assert.NotNull(upd_option);
            upd_option.AreEqual(option);
            upd_option.CodeName.AreEqualIgnoringCase(codeName);
            upd_option.CodeValue.AreEqualIgnoringCase(codeValue);
            upd_option.Price.AreEqual(price);
            upd_option.Flags.AreEqual(SalesOptionFlags.Item);

            Processor.Execute(new DeleteSalesOption
            {
                EntityID = option.EntityID
            });
        }

        [TestCase("name", "")]
        [TestCase("", "value")]
        public void Cannot_CreateUpdate_If_CodeNameOrCodeValue_Empty(string codeName, string codeValue)
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new CreateSalesOption
            {
                CodeName = codeName,
                CodeValue = codeValue
            }));
            Assert.Throws<ApplicationException>(() => Processor.Execute(new UpdateSalesOption
            {
                CodeName = codeName,
                CodeValue = codeValue
            }));
        }
    }
}
